/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.app.service;

import java.util.ArrayList;
import java.util.List;

import com.thinkgem.jeesite.modules.app.entity.OrderGoods2;
import com.thinkgem.jeesite.modules.menu.entity.Order3;
import com.thinkgem.jeesite.modules.menu.entity.OrderGoods3;
import com.thinkgem.jeesite.modules.menu.service.OrderGoods3Service;
import com.thinkgem.jeesite.modules.shop.service.OrderGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.app.entity.Order2;
import com.thinkgem.jeesite.modules.app.dao.Order2Dao;

/**
 * 订单Service
 * @author dby
 * @version 2020-07-14
 */
@Service
@Transactional(readOnly = true)
public class Order2Service extends CrudService<Order2Dao, Order2> {

	@Autowired
	private OrderGoodsService orderGoodsService;


	@Autowired
	private OrderGoods2Service orderGoods2Service;

	public Order2 get(String id) {
		return super.get(id);
	}
	
	public List<Order2> findList(Order2 order2) {
		return super.findList(order2);
	}
	
	public Page<Order2> findPage(Page<Order2> page, Order2 order2) {
		return super.findPage(page, order2);
	}
	
	@Transactional(readOnly = false)
	public void save1(Order2 order2) {
		super.save(order2);
	}
	
	@Transactional(readOnly = false)
	public void delete(Order2 order2) {
		super.delete(order2);
	}


	/*
	 * @Author Zhang Xingliang
	 * @Description 下单 同时保存多个订单商品
	 * @Date 15:07 2020/7/12
	 **/
	@Transactional(readOnly = false)
	public void saveOrder(Order2 order2) throws Exception{
		super.save(order2);

		List<OrderGoods2> order2GoodsList = order2.getOrderGoodsList();
		if(null == order2GoodsList || order2GoodsList.isEmpty()){
			throw new Exception("缺少订单商品！");
		}

		List<OrderGoods2> OrderGoods2InsertList = new ArrayList<>();

		List<OrderGoods2> OrderGoods2UpdateList = new ArrayList<>();

		for (OrderGoods2 orderGoods2 :order2.getOrderGoodsList()) {
			if(orderGoods2.getIsNewRecord()){
				orderGoods2.preInsert();
				OrderGoods2InsertList.add(orderGoods2);
			}else{
				orderGoods2.preUpdate();
//				orderGoods3Service.save(orderGoods3);
				OrderGoods2UpdateList.add(orderGoods2);
			}
			/*if(DEL_FLAG_DELETE.equals(orderGoods3.getDelFlag())){
				orderGoods3.preUpdate();
			}*/
			orderGoods2.setOrderId(order2.getId());
		}
		//保存多个订单商品
		orderGoods2Service.insertList(OrderGoods2InsertList);
		//批量更新
		orderGoods2Service.updateList(OrderGoods2UpdateList);
	}




}